<template>
  <div class="echart-container">
    <div
      class="chart-box"
      ref="chart_box"
    ></div>
  </div>
</template>

<script lang="ts">
import { Vue, Component } from 'vue-property-decorator'
import { debounce } from '@/utils/Tools'

@Component({
  name: 'Echart'
})
export default class Echart extends Vue {
  private chartEl: any = undefined
  private mounted() {
    this.initChart()
      .addEvent()
  }
  private initChart() {
    this.chartEl = this.$echarts.init(this.$refs['chart_box'])
    const option = {
      title: {
        text: '堆叠区域图',
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'cross',
          label: {
            backgroundColor: '#6a7985',
          },
        },
      },
      legend: {
        data: ['邮件营销', '联盟广告', '视频广告', '直接访问', '搜索引擎'],
      },
      toolbox: {
        feature: {
          saveAsImage: {},
        },
      },
      grid: [
        { x: '5%', y: '5%', width: '100%', height: '50%' },
        { x: '5%', y2: '5%', width: '100%', height: '45%' },
      ],
      xAxis: [
        {
          gridIndex: 0,
          type: 'category',
          boundaryGap: false,
          data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
        },
        {
          data: ['一', '二', '三', '四', '五', '六', '日'],
          gridIndex: 1,
        },
      ],
      yAxis: [
        {
          type: 'value',
        },
        {
          gridIndex: 1,
          type: 'value',
        },
      ],
      series: [
        {
          name: '邮件营销',
          type: 'line',
          data: [120, 132, 101, 134, 90, 230, 210],
          symbol: 'none',
        },
        {
          name: '联盟广告',
          type: 'line',
          data: [220, 182, 191, 234, 290, 330, 310],
          symbol: 'none',
        },
        {
          name: '视频广告',
          type: 'line',
          data: [150, 232, 2, 154, 300, 330, 410],
          symbol: 'none',
        },
        {
          name: '直接访问',
          type: 'line',
          data: [320, 12, 301, 0, 800, 330, 320],
          symbol: 'none',
        },
        {
          name: '搜索引擎',
          type: 'line',
          data: [820, 932, 901, 934, 1290, 1330, 1320],
          symbol: 'none',
        },
        {
          name: '搜索引擎',
          type: 'bar',
          data: [820, 932, 901, 934, 1290, 1330, 1320],
          xAxisIndex: 1,
          yAxisIndex: 1,
        },
        {
          name: '直接访问',
          type: 'bar',
          data: [320, 332, 301, 334, 390, 330, 320],
          xAxisIndex: 1,
          yAxisIndex: 1,
        },
      ],
    }
    this.chartEl.setOption(option)
    return this
  }
  private addEvent() {
    window.addEventListener('resize', this.resize, false)
  }
  @debounce(300)
  private resize() {
    this.chartEl.resize()
  }
}
</script>

<style lang="scss" scoped>
@mixin fullScreen() {
  height: 100%;
  width: 100%;
}
.echart-container {
  &,
  .chart-box {
    @include fullScreen();
  }
}
</style>
